<template>
  <el-dialog
    :title="type==2? '新增季后赛/总决赛数据' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="进攻效率" prop="attackEfficiency">
      <el-input v-model="dataForm.attackEfficiency" placeholder="进攻效率"></el-input>
    </el-form-item>
    <el-form-item label="防守效率" prop="defenseEfficiency">
      <el-input v-model="dataForm.defenseEfficiency" placeholder="防守效率"></el-input>
    </el-form-item>
    <el-form-item label="真实投篮命中率(%)" prop="trueShot">
      <el-input v-model="dataForm.trueShot" placeholder="真实投篮命中率"></el-input>
    </el-form-item>
    <el-form-item label="球员使用率(%)" prop="utilization">
      <el-input v-model="dataForm.utilization" placeholder="球员使用率"></el-input>
    </el-form-item>
    <el-form-item label="球员效率值" prop="per">
      <el-input v-model="dataForm.per" placeholder="球员效率值"></el-input>
    </el-form-item>
    <el-form-item label="赛季" prop="season">
      <el-input v-model="dataForm.season" placeholder="赛季"></el-input>
    </el-form-item>
    <el-form-item label="赛程" prop="stage">
      <el-input v-model="dataForm.stage" placeholder="常规赛(0)/季后赛(1)/总决赛(2)"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        type:'',
        visible: false,
        dataForm: {
          id: '',
          playerId: '',
          attackEfficiency: '',
          defenseEfficiency: '',
          trueShot: '',
          utilization: '',
          per: '',
          season: '',
          stage: ''
        },
        dataRule: {
          playerId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          attackEfficiency: [
            { required: true, message: '进攻效率不能为空', trigger: 'blur' }
          ],
          defenseEfficiency: [
            { required: true, message: '防守效率不能为空', trigger: 'blur' }
          ],
          trueShot: [
            { required: true, message: '真实投篮命中率不能为空', trigger: 'blur' }
          ],
          utilization: [
            { required: true, message: '球员使用率不能为空', trigger: 'blur' }
          ],
          per: [
            { required: true, message: '球员效率值不能为空', trigger: 'blur' }
          ],
          season: [
            { required: true, message: '赛季不能为空', trigger: 'blur' }
          ],
          stage: [
            { required: true, message: '常规赛/季后赛/总决赛不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id,type) {
        this.type = type
        console.log(type)
        if(type == 1){
        this.dataForm.id = id
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/player/playeradvanced/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.playerId = data.playerAdvanced.playerId
                this.dataForm.attackEfficiency = data.playerAdvanced.attackEfficiency
                this.dataForm.defenseEfficiency = data.playerAdvanced.defenseEfficiency
                this.dataForm.trueShot = data.playerAdvanced.trueShot
                this.dataForm.utilization = data.playerAdvanced.utilization
                this.dataForm.per = data.playerAdvanced.per
                this.dataForm.season = data.playerAdvanced.season
                this.dataForm.stage = data.playerAdvanced.stage
              }
            })
          }
        })
        }else{
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
          this.$http({
          url: this.$http.adornUrl(`/player/playeradvanced/getPlayerId/${id}`),
          method: 'get',
          }).then(({data}) => {
          this.dataForm.playerId = data.playerId
        })
        }

      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/player/playeradvanced/${this.type==2 ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'playerId': this.dataForm.playerId,
                'attackEfficiency': this.dataForm.attackEfficiency,
                'defenseEfficiency': this.dataForm.defenseEfficiency,
                'trueShot': this.dataForm.trueShot,
                'utilization': this.dataForm.utilization,
                'per': this.dataForm.per,
                'season': this.dataForm.season,
                'stage': this.dataForm.stage
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
